package com.dianning.web.action;

import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.*;
import com.dianning.bean.Visitor;
import com.dianning.fio.AvatarUploadFile;
import com.dianning.fio.IdCardUploadFile;
import com.dianning.fio.InfoPicUploadFile;
import com.dianning.fio.LicenseUploadFile;
import com.dianning.fio.QuanUploadFile;
import com.dianning.fio.UploadFile;
import com.dianning.service.VisitorService;
import com.dianning.web.interceptor.UserSessionAware;
import com.dianning.web.servlet.UserSession;

public class UpPhoto extends BaseAction implements UserSessionAware {

	private static final Log logger = LogFactory.getLog(UpPhoto.class);

	private AvatarUploadFile avatarUploadFile;
	private QuanUploadFile quanUploadFile;
	private IdCardUploadFile idCardUploadFile;
	private LicenseUploadFile licenseUploadFile;
	private InfoPicUploadFile infoPicUploadFile;

	public AvatarUploadFile getAvatarUploadFile() {
		return avatarUploadFile;
	}

	public void setAvatarUploadFile(AvatarUploadFile avatarUploadFile) {
		this.avatarUploadFile = avatarUploadFile;
	}

	public QuanUploadFile getQuanUploadFile() {
		return quanUploadFile;
	}

	public void setQuanUploadFile(QuanUploadFile quanUploadFile) {
		this.quanUploadFile = quanUploadFile;
	}

	public IdCardUploadFile getIdCardUploadFile() {
		return idCardUploadFile;
	}

	public void setIdCardUploadFile(IdCardUploadFile idCardUploadFile) {
		this.idCardUploadFile = idCardUploadFile;
	}

	public LicenseUploadFile getLicenseUploadFile() {
		return licenseUploadFile;
	}

	public void setLicenseUploadFile(LicenseUploadFile licenseUploadFile) {
		this.licenseUploadFile = licenseUploadFile;
	}

	public InfoPicUploadFile getInfoPicUploadFile() {
		return infoPicUploadFile;
	}

	public void setInfoPicUploadFile(InfoPicUploadFile infoPicUploadFile) {
		this.infoPicUploadFile = infoPicUploadFile;
	}

	private UserSession userSession;

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}

	private File qqfile;
	private String qqfileFileName;
	private String qqfileContentType;

	public File getQqfile() {
		return qqfile;
	}

	public void setQqfile(File qqfile) {
		this.qqfile = qqfile;
	}

	public String getQqfileFileName() {
		return qqfileFileName;
	}

	public void setQqfileFileName(String qqfileFileName) {
		this.qqfileFileName = qqfileFileName;
	}

	public String getQqfileContentType() {
		return qqfileContentType;
	}

	public void setQqfileContentType(String qqfileContentType) {
		this.qqfileContentType = qqfileContentType;
	}

	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return ERROR;
		}
	}

	public String infoPic() {
		if (userSession.getUid() == 0) {
			this.message = "{success:false,error:'用户未登陆'}";
		} else {
			UploadFile picFile = null;
			if (this.getQqfile() != null
					&& StringUtils.isNotBlank(this.getQqfileFileName())) {
				try {
					picFile = new UploadFile();
					picFile.setFileName(this.getQqfileFileName());
					picFile.setInputStream(new FileInputStream(this.getQqfile()));
					String path = this.getInfoPicUploadFile().saveUploadFile(picFile);
					this.message = "{success:true,img:'/" + path + "'}";
				} catch (Exception ex) {
					logger.info(ex);
					picFile = null;
				}
			} else {
				this.message = "{success:false,error:'图片有错误'}";
			}
		}
		return SUCCESS;
	}

	public String quanPic() {
		if (userSession.getUid() == 0) {
			this.message = "{success:false,error:'用户未登陆'}";
		} else {
			UploadFile picFile = null;
			if (this.getQqfile() != null
					&& StringUtils.isNotBlank(this.getQqfileFileName())) {
				try {
					picFile = new UploadFile();
					picFile.setFileName(this.getQqfileFileName());
					picFile.setInputStream(new FileInputStream(this.getQqfile()));
					String path = this.getQuanUploadFile().saveUploadFile(picFile);
					this.message = "{success:true,img:'/" + path + "'}";
				} catch (Exception ex) {
					picFile = null;
				}
			} else {
				this.message = "{success:false,error:'图片有错误'}";
			}
		}
		return SUCCESS;
	}

	public String avatar() {
		if (userSession.getUid() == 0) {
			this.message = "{success:false,error:'用户未登陆'}";
		} else {
			UploadFile picFile = null;
			if (this.getQqfile() != null
					&& StringUtils.isNotBlank(this.getQqfileFileName())) {
				try {
					picFile = new UploadFile();
					picFile.setFileName(this.getQqfileFileName());
					picFile.setInputStream(new FileInputStream(this.getQqfile()));
					this.getAvatarUploadFile().saveUploadFile(picFile,userSession.getUid());
					this.message = "{success:true,img:'" + userSession.getUid() + "'}";
				} catch (Exception ex) {
					picFile = null;
				}
			} else {
				this.message = "{success:false,error:'图片有错误'}";
			}
		}
		return SUCCESS;
	}

	public String idcard() {
		if (userSession.getUid() == 0) {
			this.message = "{success:false,error:'用户未登陆'}";
		} else {
			UploadFile picFile = null;
			if (this.getQqfile() != null
					&& StringUtils.isNotBlank(this.getQqfileFileName())) {
				try {
					picFile = new UploadFile();
					picFile.setFileName(this.getQqfileFileName());
					picFile.setInputStream(new FileInputStream(this.getQqfile()));
					this.getIdCardUploadFile().saveUploadFile(picFile,userSession.getUid());
					this.message = "{success:true,img:'" + userSession.getUid() + "'}";
				} catch (Exception ex) {
					picFile = null;
				}
			} else {
				this.message = "{success:false,error:'图片有错误'}";
			}
		}
		return SUCCESS;
	}

	public String license() {
		if (userSession.getUid() == 0) {
			this.message = "{success:false,error:'用户未登陆'}";
		} else {
			UploadFile picFile = null;
			if (this.getQqfile() != null
					&& StringUtils.isNotBlank(this.getQqfileFileName())) {
				try {
					picFile = new UploadFile();
					picFile.setFileName(this.getQqfileFileName());
					picFile.setInputStream(new FileInputStream(this.getQqfile()));
					this.getLicenseUploadFile().saveUploadFile(picFile,userSession.getUid());
					this.message = "{success:true,img:'" + userSession.getUid() + "'}";
				} catch (Exception ex) {
					picFile = null;
				}
			} else {
				this.message = "{success:false,error:'图片有错误'}";
			}
		}
		return SUCCESS;
	}

}
